package com.mec.web.y2012.coreservlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.mec.beans.y2012.coreservlet.BidInfo;

/**
 * Servlet implementation class BidServlet
 */
@WebServlet("/BidServlet")
public class BidServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public BidServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		BidInfo bid = new BidInfo();
		BeanUtilities.populateBean(bid, request);
		if(bid.isComplete()){
			//All required form data was supplied, show result
			showBid(request, response, bid);
		}else{
			showEntryForm(request, response, bid);
		}
	}
	
	private void showBid(HttpServletRequest request, HttpServletResponse response, BidInfo bid) throws ServletException, IOException{
		submitBid(bid);
		//
		response.setContentType("text/html");
		//
		PrintWriter out = response.getWriter();
		
		//
		String title = "Bid Submitted";
		out.println(Constants.docType + ServletUtilities.headWithTitle(title) + 
				"<body bgColor = \"#fdf5e6\"><center><h1>" + title + "</h1></center>" +
				"<p>Your bid is now active, if your bid is successful, \n" +
				"you will be notified within 24 hours of the close\n of bidding.\n" +
				"</p>\n" +
				"<table border=\"1\"><tr><th bgcolor=\"black\" style=\"font-color:white;background-color:grey\">"  +
				bid.getItemName() + "</th></tr><tr><th>Item ID:" +
				bid.getItemId() + "</th></tr><tr><th>Name: " + 
				bid.getBidderName() + "</th></tr><tr><th>Email Address: " +
				bid.getEmailAddress() + "</th></tr><tr><th>Bid Price: " + 
				bid.getBidPrice() + "</th></tr><tr><th>Auto-increment Price: " +
				bid.isAutoIncrement() + "</th></tr></table><body></html>"
				);
	}
	
	private void showEntryForm(HttpServletRequest request, HttpServletResponse response, BidInfo bid) throws ServletException, IOException{
		boolean isPartlyComplete = bid.isPartlyComplete();
		response.setContentType("text/html");
		//
		PrintWriter out = response.getWriter();
		String title = "Welcom to Aucitons-R-US. Please Enter Bid.";
		out.println(Constants.docType + ServletUtilities.headWithTitle(title) +
				"<body bgColor=\"#fdf5e6\"><center><h1>" + title + "</h1></center>" +
				warning(isPartlyComplete) + "<form>" + 
				inputElement("Item ID", "itemId", bid.getItemId(), isPartlyComplete	) +
				inputElement("Item Name", "itemName", bid.getItemName(), isPartlyComplete) +
				inputElement("Bidder Name", "bidderName", bid.getBidderName(), isPartlyComplete) +
				inputElement("Your Email Address", "emailAddress", bid.getEmailAddress(), isPartlyComplete) +
				inputElement("Amount Bid", "bidPrice", bid.getBidPrice(), isPartlyComplete) +
				checkBox("Auto-increment bid to match other bidders?", "autoIncrement", bid.isAutoIncrement()) +
				"<input type=\"submit\" value=\"Submit Bid\"/></body></html>"
				);
		
	}
	
	private String inputElement(String title, String name, String value, boolean isPartlyComplete){
		String message = "";
		if(isPartlyComplete && (null == value || value.equals(""))){
			message = "<strong>Required field!</strong>";
		}
		//
		return message + title + ":<input type=\"text\" name=\"" + name + "\" value=\"" + value + "\"/><br/>\n";
	}
	
	private String inputElement(String title, String name, Double value, boolean isPartlyComplete){
		String num;
		if(value == 0.0){
			num = "";
		}else{
			num = value.toString();
		}
		return inputElement(title, name, num, isPartlyComplete);
		
	}
	private String checkBox(String title, String name, boolean isChecked){
		String result = title + ":<input type=\"checkbox\" name=\"" + name + "\"";
		if(isChecked){
			result = result + " checked";
		}
		//
		result += "/><br/>\n";
		return result;
	}
	
	private String warning(boolean isPartlyComplete){
		if(isPartlyComplete){
			return "<h2>Required Data Missing! Enter and Resubmit.</h2>\n" ;
		}else{
			return "";
		}
	}
	
	private void submitBid(BidInfo bid){
		//application-specific logic here;
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	private static final Logger logger = 
			Logger.getLogger(BidServlet.class);
}
